﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Common.Service;
using OC.Common.Entity;

namespace OC
{
    public partial class UserGroupManage : System.Web.UI.Page
    {
        OC.Common.Interface.IDictionary _dictionaryService;

        public OC.Common.Interface.IDictionary DictionaryService
        {
            get
            {
                if (_dictionaryService == null)
                {
                    _dictionaryService = ServiceContainer.GetService<OC.Common.Interface.IDictionary>();
                }
                return _dictionaryService;
            }
            set { _dictionaryService = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitUserGroupView();
            }
        }

        private void InitUserGroupView()
        {
            IList userGroups = DictionaryService.GetListByDictionaryType("DictionaryLogic_GetListByDictionaryType", "USERGROUP");
            tvUserGroup.Nodes.Clear();
            foreach (DictionaryEntity item in userGroups)
            {
                if (item.PARENT_DICTIONARY_ID.Trim() == "0")//先找到根节点
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = item.CN_NAME;
                    tn.Value = item.DICTIONARY_ID;
                    GetChildNodeByList(userGroups, item.DICTIONARY_ID, tn);
                    tvUserGroup.Nodes.Add(tn);
                }
            }
            tvUserGroup.ExpandDepth = 1;
        }

        /// <summary>
        /// 根据ID在查询到的组织数据中轮询查找它的子节点
        /// </summary>
        /// <param name="structure"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private TreeNode GetChildNodeByList(IList structure, string id, TreeNode ptn)
        {
            foreach (DictionaryEntity item in structure)
            {
                if (id == item.PARENT_DICTIONARY_ID)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = item.CN_NAME;
                    tn.Value = item.DICTIONARY_ID;
                    ptn.ChildNodes.Add(tn);
                    foreach (DictionaryEntity citem in structure)
                    {
                        if (item.DICTIONARY_ID == citem.PARENT_DICTIONARY_ID)
                        {
                            TreeNode ctn = new TreeNode();
                            ctn.Text = citem.CN_NAME;
                            ctn.Value = citem.DICTIONARY_ID;
                            tn.ChildNodes.Add(ctn);
                        }
                    }
                }
            }
            return ptn;
        }

        protected void tvUserGroup_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            TreeNode tn = e.Node;
            if (tn.ChildNodes.Count > 0)
            {
                foreach (TreeNode item in tn.ChildNodes)
                {
                    if (item.ChildNodes.Count <= 0)
                    {
                        IList dicEntitys = DictionaryService.GetList(item.Value);
                        foreach (DictionaryEntity dics in dicEntitys)
                        {
                            TreeNode ctn = new TreeNode();
                            ctn.Text = dics.CN_NAME;
                            ctn.Value = dics.DICTIONARY_ID;
                            item.ChildNodes.Add(ctn);
                        }
                    }
                }
            }
        }

        protected void tvUserGroup_SelectedNodeChanged(object sender, EventArgs e)
        {
            ViewState["AppendChild"] = null;
            panelAppendChild.Visible = false;
            panelRoleInfo.Visible = true;
            lblTitle.Text = "角色信息";
            TreeNode tn = tvUserGroup.SelectedNode;
            DictionaryEntity dicEntity = DictionaryService.Get(tn.Value);
            if (dicEntity != null)
            {
                txtGroupName.Text = dicEntity.CN_NAME;
                txtUpGroup.Text = tn.Parent != null ? tn.Parent.Text : string.Empty;
                txtDescription.Text = dicEntity.EXT15;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            TreeNode tn = tvUserGroup.SelectedNode;
            DictionaryService.DeleteDictionaryByID(tn.Value);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DictionaryEntity dicEntity = new DictionaryEntity();
            dicEntity.DICTIONARY_ID = tvUserGroup.SelectedValue;
            dicEntity.UPDATE_DATE = DateTime.Now;
            dicEntity.UPDATE_BY = "";
            dicEntity.EXT15 = txtDescription.Text;
            dicEntity.CN_NAME = txtGroupName.Text;
            //进行Update操作
            DictionaryService.UpdateDictionary(dicEntity);
            tvUserGroup.SelectedNode.Text = txtGroupName.Text;
        }

        protected void btnAppendChild_Click(object sender, EventArgs e)
        {
            if (panelAppendChild.Visible)
            {
                return;
            }
            panelAppendChild.Visible = true;
            panelRoleInfo.Visible = false;
            lblTitle.Text = "新增角色";

            txtAppendDescription.Text = string.Empty;
            txtAppendCNName.Text = string.Empty;
            DictionaryEntity dicEntity = new DictionaryEntity();
            if (tvUserGroup.SelectedNode != null)
            {
                txtAppendUpGroup.Text = tvUserGroup.SelectedNode.Text;
                dicEntity.PARENT_DICTIONARY_ID = tvUserGroup.SelectedValue;
            }
            else
            {
                dicEntity.PARENT_DICTIONARY_ID = "0";
            }
            dicEntity.DICTIONARY_TYPE = "USERGROUP";
            dicEntity.IS_ACTIVE = "1";
            dicEntity.ORDER_NO = 1;
            dicEntity.DICTIONARY_CODE = "";
            ViewState["AppendChild"] = dicEntity;
        }

        protected void btnAppend_Click(object sender, EventArgs e)
        {
            DictionaryEntity dicEntity = ViewState["AppendChild"] as DictionaryEntity;
            dicEntity.DICTIONARY_ID = Guid.NewGuid().ToString();
            dicEntity.CREATE_BY = "";
            dicEntity.CREATE_DATE = DateTime.Now;
            dicEntity.UPDATE_BY = "";
            dicEntity.UPDATE_DATE = DateTime.Now;
            dicEntity.CN_NAME = txtAppendCNName.Text;
            dicEntity.EN_NAME = txtAppendENName.Text;
            dicEntity.EFFECT_FROM = DateTime.Parse(txtBeginDate.Text);
            dicEntity.EFFECT_TO = DateTime.Parse(txtEndDate.Text);
            //dicEntity.EXT01 = ddlJob.SelectedValue;
            dicEntity.EXT15 = txtAppendDescription.Text;
            DictionaryService.InsertDictionary(dicEntity);
            panelAppendChild.Visible = false;
            panelRoleInfo.Visible = true;
            lblTitle.Text = "角色信息";


            //在树上新增一个节点
            TreeNode ctn = new TreeNode();
            ctn.Text = txtAppendCNName.Text;
            ctn.Value = dicEntity.DICTIONARY_ID;
            tvUserGroup.SelectedNode.ChildNodes.Add(ctn);
            //InitUserGroupView();
            tvUserGroup.ExpandDepth = 2;
            //清空控件内容
            txtAppendCNName.Text = string.Empty;
            txtAppendENName.Text = string.Empty;
            txtAppendUpGroup.Text = string.Empty;
            txtBeginDate.Text = string.Empty;
            txtEndDate.Text = string.Empty;
            txtAppendDescription.Text = string.Empty;
            //ddlJob.SelectedIndex = 0;

        }
    }
}